package com.lagou.controller;


import com.lagou.domain.*;
import com.lagou.service.MenuService;
import com.lagou.service.RoleService;
import com.sun.org.apache.bcel.internal.generic.PUSH;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {


    @Autowired
    private RoleService roleService;


    @Autowired
    private MenuService menuService;
    /*
     * 角色列表查询&条件查询
     * */
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role){

        List<Role> allRole = roleService.findAllRole(role);
        ResponseResult responseResult = new ResponseResult(true,200,"查询所有角色成功",allRole);
        return responseResult;
    }

    /*查询所有父子菜单信息*/
    @RequestMapping("/findAllMenu")
    public ResponseResult findSubMenuListByPid() {

        //-1 表示查询所有菜单数据
        List<Menu> subMenuListByPid = menuService.findSubMenuListByPid (-1);

        Map<String,Object> map = new HashMap ();

        map.put ("parentMenuList",subMenuListByPid);

        ResponseResult result = new ResponseResult (true, 200, "查询成功", map);

        return result;

    }

    /*
     * 根据角色id查询该角色关联的菜单信息
     * */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {

        List<Integer> menuByRoleId = roleService.findMenuByRoleId (roleId);

        ResponseResult result = new ResponseResult (true, 200, "响应成功", menuByRoleId);

        return result;
    }

    /*为角色分配菜单信息*/
    @RequestMapping("/RoleContextMenu")
    public ResponseResult RoleContextMenu(@RequestBody RoleMenuVo roleMenuVo) {

        roleService.RoleContextMenu (roleMenuVo);

        ResponseResult result = new ResponseResult (true, 200, "响应成功", "");

        return result;
    }

    /*删除角色*/
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id) {

        roleService.deleteRole (id);

        ResponseResult result = new ResponseResult (true, 200, "删除成功", "");

        return result;
    }

    /*查询该角色拥有的资源分类信息和资源信息*/
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId) {

        List<ResourceCategory> resourceCategoryList = roleService.findResourceListByRoleId (roleId);

        ResponseResult result = new ResponseResult (true, 200, "响应成功", resourceCategoryList);

        return result;
    }

    /*为角色分配资源关系*/
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVo roleResourceVo) {

        roleService.roleContextResource (roleResourceVo);

        ResponseResult result = new ResponseResult (true, 200, "新关系建立成功", null);

        return result;
    }
}

